/*
 * TimeTableModel.java
 *
 * Created on 14 July 2003, 08:58
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui.time;

import com.velocityme.client.gui.VectorTableModel;
import com.velocityme.session.NodeSessionBean;
import com.velocityme.utility.DurationFormat;
import com.velocityme.valueobjects.StateValueToString;
import com.velocityme.valueobjects.TimeValue;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

/**
 *
 * @author  Robert Crida Work
 */
public class TimeTableModel extends VectorTableModel {
    
    /** Object used for formatting the time */
    DateFormat m_dateFormatter;

    /** Creates a new instance of TimeTableModel */
    public TimeTableModel() {
        super(new String[] {"Person", "Description", "State", "Start", "Duration"});
        m_dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");;
    }
    
    public void setTimeValues(Collection p_values) {
        m_dataRows.clear();
        Iterator i = p_values.iterator();
        while(i.hasNext()) {
            TimeValue value = (TimeValue)i.next();
            String person = value.getPersonValue().getContactableValue().getNodeValue().getName();
            String description = value.getDescription();
            String state = StateValueToString.toString(value.getStateValue());
            Date start = value.getStartTime();
            long duration = value.getStopTime().getTime() - value.getStartTime().getTime();
            String startStr = m_dateFormatter.format(start);
            String durationStr = DurationFormat.formatDuration(duration);
            m_dataRows.addElement(new Object[]{ person, description, state, startStr, durationStr, value });
        }
        fireTableDataChanged();
    }
    
    public TimeValue getValue(int row) {
        return (TimeValue)getValueAt(row, 5);
    }

}
